//
//  AY_SearchCell.m
//  AiYue
//
//  Created by mac on 2023/11/10.
//

#import "AY_SearchCell.h"

@interface AY_SearchCell ()

@property (nonatomic, strong) UIImageView *bookImg;
@property (nonatomic, strong) UILabel *bookName;
@property (nonatomic, strong) UILabel *bookContent;
@property (nonatomic, strong) UILabel *bookAuthor;
@property (nonatomic, strong) UILabel *tipLabel;

@end

@implementation AY_SearchCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bookImg = [AYTool createImageViewWithFrame:CGRectZero andImage:nil andBg:nil andRadius:YNUM(4) andAddSubView:self.contentView];
        [self.bookImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(YNUM(16));
            make.width.mas_equalTo(YNUM(80));
            make.height.mas_equalTo(YNUM(106));
        }];
        
        self.tipLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:[UIColor whiteColor] andBgColor:nil andFont:YFont(10) andLines:1 andRadius:0 andAddSubView:self.bookImg];
        self.tipLabel.layer.masksToBounds = NO;
        self.tipLabel.textAlignment = NSTextAlignmentCenter;
        [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.equalTo(self.bookImg);
            make.width.mas_equalTo(YNUM(28));
            make.height.mas_equalTo(YNUM(16));
        }];
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, YNUM(28), YNUM(16)) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(YNUM(4), YNUM(4))];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.tipLabel.bounds;
        maskLayer.path = maskPath.CGPath;
        self.tipLabel.layer.mask = maskLayer;
        
        self.bookName = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self.contentView];
        [self.bookName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookImg.mas_right).offset(YNUM(12));
            make.right.equalTo(self.contentView).offset(-YNUM(16));
            make.top.equalTo(self.bookImg);
        }];
        
        self.bookContent = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#97A4B0") andBgColor:nil andFont:YFont(12) andLines:2 andRadius:0 andAddSubView:self.contentView];
        [self.bookContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookName);
            make.right.equalTo(self.contentView).offset(-YNUM(16));
            make.top.equalTo(self.bookName.mas_bottom).offset(YNUM(6));
        }];
        
        self.bookAuthor = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#646D75") andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:self.contentView];
        [self.bookAuthor mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookName);
            make.right.equalTo(self.contentView).offset(-YNUM(16));
            make.bottom.equalTo(self.bookImg).offset(-YNUM(6));
        }];
    }
    return self;
}

- (void)sendModel:(SearchModel *)model andSearchKey:(NSString *)searchKey {
//    self.bookName.text = model.title;
    [self.bookImg sd_setImageWithURL:[NSURL URLWithString:model.coverImg] placeholderImage:YImage(@"placeholderImage")];
    self.bookContent.text = model.info;
//    self.bookAuthor.text = kStringIsEmpty(model.lead) ? model.author : model.lead;
    NSString *author = kStringIsEmpty(model.lead) ? model.author : model.lead;
    [AYTool setBookStatusWithStatus:model.status andLabel:self.tipLabel];
    
    NSString *title = kStringIsEmpty(model.articleSecondaryTitle) ? model.title : model.articleSecondaryTitle;
    if ([title rangeOfString:searchKey].location != NSNotFound) {
        NSRange range = [title rangeOfString:searchKey];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:title];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        self.bookName.attributedText = attributedString;
    }else {
        self.bookName.textColor = COLOR(@"#000002");
        self.bookName.text = title;
    }
    
    if ([author rangeOfString:searchKey].location != NSNotFound) {
        NSRange range1 = [author rangeOfString:searchKey];
        NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:author];
        [attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range1];
        self.bookAuthor.attributedText = attributedString1;
    }else {
        self.bookAuthor.textColor = COLOR(@"#000002");
        self.bookAuthor.text = author;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
